<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{$user.id ? '编辑用户' : '新建用户'}</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: 'Microsoft YaHei', Arial, sans-serif; background-color: #f5f5f5; height: 100vh; display: flex; align-items: center; justify-content: center; }
        .form-container { background: white; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); padding: 30px; width: 100%; max-width: 500px; }
        h2 { color: #333; margin-bottom: 20px; text-align: center; }
        .form-group { margin-bottom: 20px; }
        .form-group label { display: block; margin-bottom: 8px; color: #555; font-weight: 500; }
        .form-group input[type="text"],
        .form-group input[type="password"],
        .form-group input[type="email"],
        .form-group select { width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 4px; font-size: 14px; }
        .form-group input:focus,
        .form-group select:focus { outline: none; border-color: #667eea; }
        .form-actions { display: flex; justify-content: center; gap: 15px; margin-top: 30px; }
        .btn { padding: 10px 20px; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; transition: all 0.3s ease; }
        .btn-primary { background: #667eea; color: white; }
        .btn-primary:hover { background: #5a6fd8; }
        .btn-cancel { background: #6c757d; color: white; }
        .btn-cancel:hover { background: #5a6268; }
        .help-text { color: #666; font-size: 12px; margin-top: 5px; }
        
        /* 提示框样式 */
        .message-box {
            position: fixed; 
            top: 50%; 
            left: 50%; 
            transform: translate(-50%, -50%); 
            background: white; 
            padding: 20px; 
            border-radius: 8px; 
            box-shadow: 0 4px 16px rgba(0,0,0,0.2); 
            z-index: 1000; 
            min-width: 300px;
            text-align: center;
            display: none;
        }
        
        .message-box.success {
            border-left: 4px solid #28a745;
        }
        
        .message-box.error {
            border-left: 4px solid #dc3545;
        }
        
        .message-content {
            margin-bottom: 15px;
            font-size: 14px;
            color: #333;
        }
        
        .message-actions {
            display: flex;
            justify-content: center;
        }
        
        .message-btn {
            padding: 6px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            background: #667eea;
            color: white;
        }
    </style>
</head>
<body>
    <!-- 提示框 -->
    <div id="messageBox" class="message-box">
        <div class="message-content"></div>
        <div class="message-actions">
            <button class="message-btn" onclick="hideMessageBox()">确定</button>
        </div>
    </div>
    
    <div class="form-container">
        <h2>{$user.id ? '编辑用户' : '新建用户'}</h2>
        
        <form id="userForm">
            <input type="hidden" name="c" value="User">
            <input type="hidden" name="a" value="update">
            {if $user.id}<input type="hidden" name="id" value="{$user.id}">{/if}
            
            <div class="form-group">
                <label for="username">用户名 *</label>
                <input type="text" id="username" name="username" value="{$user.username}" required placeholder="请输入用户名">
            </div>
            
            <div class="form-group">
                <label for="email">邮箱 *</label>
                <input type="email" id="email" name="email" value="{$user.email}" required placeholder="请输入邮箱地址">
            </div>
            
            <div class="form-group">
                <label for="password">密码 {if $user.id}（留空则不修改）{else}*（默认123456）{/if}</label>
                <input type="password" id="password" name="password" placeholder="请输入密码">
                {if $user.id}<div class="help-text">留空表示不修改当前密码</div>{/if}
            </div>
            
            <div class="form-group">
                <label for="status">状态</label>
                <select id="status" name="status">
                    <option value="1" {if $user.status == '1' || $user.status == 'active' || $user.status == '正常'}selected{/if}>正常</option>
                    <option value="0" {if $user.status == '0' || $user.status == 'disabled' || $user.status == '禁用'}selected{/if}>禁用</option>
                </select>
            </div>
            
            <div class="form-actions">
                <button type="submit" class="btn btn-primary">保存</button>
                <button type="button" class="btn btn-cancel" onclick="window.location.href='/admin.php?c=User&a=user_manage'">取消</button>
            </div>
        </form>
    </div>
    <script>
        // 显示提示框
        function showMessageBox(message, isSuccess = true) {
            const messageBox = document.getElementById('messageBox');
            const messageContent = messageBox.querySelector('.message-content');
            
            messageContent.textContent = message;
            
            // 设置提示框样式
            if (isSuccess) {
                messageBox.classList.add('success');
                messageBox.classList.remove('error');
            } else {
                messageBox.classList.add('error');
                messageBox.classList.remove('success');
            }
            
            // 显示提示框
            messageBox.style.display = 'block';
        }
        
        // 隐藏提示框
        function hideMessageBox() {
            document.getElementById('messageBox').style.display = 'none';
        }
        
        // AJAX请求函数，支持GET和POST方法
        function ajaxRequest(url, params, successCallback, errorCallback, method = 'GET') {
            const xhr = new XMLHttpRequest();
            
            if (method === 'GET') {
                const queryString = Object.keys(params).map(key => 
                    encodeURIComponent(key) + '=' + encodeURIComponent(params[key])
                ).join('&');
                xhr.open('GET', url + '?' + queryString, true);
            } else {
                xhr.open('POST', url, true);
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            }
            
            xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
            
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        try {
                            const response = JSON.parse(xhr.responseText);
                            if (successCallback) {
                                successCallback(response);
                            }
                        } catch (e) {
                            if (errorCallback) {
                                errorCallback('解析响应失败');
                                console.error('JSON解析错误:', e);
                            }
                        }
                    } else {
                        if (errorCallback) {
                            errorCallback('请求失败，状态码：' + xhr.status);
                        }
                    }
                }
            };
            
            // 发送数据
            if (method === 'POST') {
                const formData = Object.keys(params).map(key => 
                    encodeURIComponent(key) + '=' + encodeURIComponent(params[key])
                ).join('&');
                xhr.send(formData);
            } else {
                xhr.send();
            }
        }
        
        // 提交用户表单
        document.getElementById('userForm').addEventListener('submit', function(event) {
            event.preventDefault();
            
            const formData = new FormData(this);
            const params = {};
            
            // 将FormData转换为普通对象
            for (const [key, value] of formData.entries()) {
                params[key] = value;
            }
            
            const username = params.username.trim();
            const email = params.email.trim();
            
            if (!username || !email) {
                showMessageBox('用户名和邮箱不能为空', false);
                return;
            }
            
            ajaxRequest(
                '/admin.php?c=User&a=update',
                params,
                function(response) {
                    if (response.success) {
                        showMessageBox(response.message);
                        
                        // 延迟返回用户管理页面，让用户看到提示信息
                        setTimeout(function() {
                            window.location.href = '/admin.php?c=User&a=user_manage';
                        }, 1500);
                    } else {
                        showMessageBox(response.message, false);
                    }
                },
                function(error) {
                    showMessageBox(error, false);
                },
                'POST'
            );
        });
    </script>
</body>
</html>